package com.fll.fangyiuser.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fll.fangyicommon.entity.ResultOk;
import com.fll.fangyicommon.entity.user.User;
import com.fll.fangyicommon.utils.JWTUtil;
import com.fll.fangyiuser.service.LoginService;
import com.fll.fangyiuser.service.OssService;
import com.fll.fangyiuser.util.SmsUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author fll
 * @create 2023/2/24
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private LoginService loginService;
    @Autowired
    private OssService ossService;
    @Autowired
    private SmsUtil smsUtil;

    //判断是否注册
    @RequestMapping("/verifyRegistered")
    public ResultOk verifyRegistered(@RequestParam("phoneNumber") String phoneNumber){
        ResultOk<Integer> ok = new ResultOk<>();
        if (phoneNumber != null){
            Boolean isRegistered = loginService.verifyRegistered(phoneNumber);
            if (isRegistered){
                ok.setCode(200);
                ok.setMsg("该号码已注册");
                ok.setData(0); //已注册
                return ok;
            } else { //未注册，进行注册
                ok.setCode(200);
                ok.setMsg("号码未注册");
                ok.setData(1); //未注册
                return ok;
            }
        }
        if (phoneNumber == null){
            ok.setCode(400);
            ok.setMsg("号码不能为空");
        }
        return ok;
    }

    //发送验证码
    @PostMapping("/sms/sendCode")
    public ResultOk sendCode(@RequestBody String phoneNumber){
        JSONObject params = JSONObject.parseObject(phoneNumber);
        String phone = params.getString("phoneNumber");
        ResultOk ok = new ResultOk();
        String[] Phones = {"+86" + phone};
        boolean sendSms = smsUtil.sendSms(Phones);
        if (sendSms){
            ok.setCode(200);
            ok.setMsg("验证码发送成功");
            return ok;
        }
        ok.setCode(400);
        ok.setMsg("验证码发送失败");
        return ok;
    }

    /**
     *  * 1账号密码登录、验证码注册合并接口
     *  * type: 0,账号密码登录
     *  * type: 1,验证码注册
     * @param
     * @param
     * @return
     */
    @PostMapping("/loginOrRegister")
    public ResultOk login(@RequestBody Map<String, String> params) throws JsonProcessingException {
        return loginService.loginOrRegister(params);
    }

    @GetMapping("/logout")
    public ResultOk logout(@RequestHeader String userInfo){

        //TODO 清除redis缓存

        ResultOk<Object> ok = new ResultOk<>();

//        User user = null;
//        try {
//            //将请求头中的user信息系列化成user对象
//            user = new ObjectMapper().readValue(userInfo, User.class);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        ok.setCode(200);
        ok.setMsg("退出成功！");
        ok.setData(null);
        log.info("退出成功！");
        return ok;
    }

    @PostMapping("/resetPass")
    public ResultOk resetPass(@RequestBody Map<String, String> params){
        return loginService.resetPasss(params);
    }


}
